<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <!--    <link rel="stylesheet" href="../plugins/elementui/index.css">-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-tree">
                <el-button type="primary" class="butT" @click="handleCreate()">新增菜单</el-button>
            </div>
            <el-table
                    current-row-key="id"
                    stripe
                    highlight-current-row
                    row-key="id"
                    :data="menuList"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                <el-table-column prop="priority" label="菜单顺序" align="center"></el-table-column>
                <el-table-column prop="icon" label="菜单图标">
                    <template slot-scope="scope">
                        <div class="size-icon">
                            <i size="medium" :class="scope.row.icon"></i>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="description" label="菜单说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleSonCreate(scope.row)">新增子菜单</el-button>
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单层级">
                                                <el-select v-model="status" :disabled="true">
                                                    <el-option label="导航菜单" value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单图标">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="父级菜单">
                                                <el-select v-model="formData.parentMenuId" placeholder="跟菜单"
                                                           :disabled="true">
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>

                                        <el-col :span="12">
                                            <el-form-item label="菜单顺序">
                                                <el-input-number size="medium" v-model="formData.priority"
                                                                 :min="1"></el-input-number>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="链接地址">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="菜单说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
                <!-- 新增子菜单标签弹层 -->
                <div class="add-form">
                    <el-dialog title="新增子菜单" :visible.sync="dialogFormVisible4Add">
                        <template>
                            <el-tabs v-model="activeName" type="card">
                                <el-tab-pane label="基本信息" name="first">
                                    <el-form :rules="rules" :model="formData" prop="name" ref="dataEditForm"
                                             label-position="right"
                                             label-width="100px">
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="菜单名称" prop="name">
                                                    <el-input v-model="formData.name"/>
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="菜单层级">
                                                    <el-select v-model="status" :disabled="true">
                                                        <el-option label="链接菜单" value="1"></el-option>
                                                    </el-select>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="菜单图标">
                                                    <el-input v-model="formData.icon"/>
                                                </el-form-item>
                                            </el-col>

                                            <el-col :span="12">
                                                <el-form-item label="父级菜单">
                                                    <el-select v-model="formData.parentMenuId" :disabled="true">
                                                        <el-option label="跟菜单" value=""></el-option>
                                                        <el-option
                                                                v-for="menu in menuList"
                                                                :key="menu.id"
                                                                :label="menu.name"
                                                                :value="menu.id">
                                                        </el-option>
                                                    </el-select>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="菜单顺序">
                                                    <el-input-number size="medium" v-model="formData.priority"
                                                                     :min="1"></el-input-number>
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="链接地址">
                                                    <el-input v-model="formData.linkUrl"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="菜单说明">
                                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                    </el-form>
                                </el-tab-pane>
                                <el-tab-pane v-if=status label="权限信息" name="second">
                                    <div class="checkScrol">
                                        <table class="datatable">
                                            <thead>
                                            <tr>
                                                <th>选择</th>
                                                <th>角色</th>
                                                <th>关键字</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr v-for="c in tableData">
                                                <td>
                                                    <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                                </td>
                                                <td><label :for="c.id">{{c.name}}</label></td>
                                                <td><label :for="c.id">{{c.keyword}}</label></td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </el-tab-pane>
                            </el-tabs>
                        </template>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogFormVisible4Add = false">取消</el-button>
                            <el-button type="primary" @click="handleAddSon()">确定</el-button>
                        </div>
                    </el-dialog>
                </div>
                <!--修改菜单-->
                <div class="add-form">
                    <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                        <template>
                            <el-tabs v-model="activeName" type="card">
                                <el-tab-pane label="基本信息" name="first">
                                    <el-form :rules="rules" :model="formData" prop="name" ref="dataEditForm"
                                             label-position="right"
                                             label-width="100px">
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="菜单名称" prop="name">
                                                    <el-input v-model="formData.name"/>
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="菜单层级">
                                                    <el-select v-model="status" :disabled="true">
                                                        <el-option label="导航菜单" value="1"></el-option>
                                                        <el-option label="链接菜单" value="0"></el-option>
                                                    </el-select>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="菜单图标">
                                                    <el-input v-model="formData.icon"/>
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="父级菜单">
                                                    <el-select
                                                            v-model="formData.parentMenuId"
                                                            placeholder="跟菜单"
                                                            :disabled="true">
                                                        <el-option label="跟菜单" value=""></el-option>
                                                        <el-option
                                                                v-for="menu in menuList"
                                                                :key="menu.id"
                                                                :label="menu.name"
                                                                :value="menu.id">
                                                        </el-option>
                                                    </el-select>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="菜单顺序">
                                                    <el-input-number size="medium" v-model="formData.priority"
                                                                     :min="1"></el-input-number>
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="链接地址">
                                                    <el-input v-model="formData.linkUrl"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="菜单说明">
                                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                    </el-form>
                                </el-tab-pane>
                                <el-tab-pane label="权限信息" name="second" v-if=status4table>
                                    <div class="checkScrol">
                                        <table class="datatable">
                                            <thead>
                                            <tr>
                                                <th>选择</th>
                                                <th>角色</th>
                                                <th>关键字</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr v-for="c in tableData">
                                                <td>
                                                    <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                                </td>
                                                <td><label :for="c.id">{{c.name}}</label></td>
                                                <td><label :for="c.id">{{c.keyword}}</label></td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </el-tab-pane>
                            </el-tabs>
                        </template>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                            <el-button type="primary" @click="handleEdit()">确定</el-button>
                        </div>
                    </el-dialog>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!--<script src="../plugins/elementui/index.js"></script>-->
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Add: false,//新增子菜单表单是否可见
            dialogFormVisible4Edit: false,//编辑菜单表单是否可见
            formData: {},//表单数据
            status: "1",//菜单层级 0：导航菜单 1：链接菜单
            status4table: false,//是否显示角色表单
            menuList: [],//菜单列表
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            roleIds: [],//新增子菜单和编辑子菜单的权限信息
            rules: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            //发送请求查询所有菜单
            this.findPage();
        },
        methods: {
            handleAdd() {
                axios.post("/menu/add", this.formData).then(res => {
                    //弹出消息
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    //关闭对话框
                    this.dialogFormVisible = false;
                    this.findPage();
                });
            },
            //发送请求查询所有菜单
            findPage() {
                axios.get("/menu/menuList").then(res => {
                        if (res.data.flag) {
                            this.menuList = res.data.data;
                        } else {
                            this.$message.error(res.data.message);
                        }
                    }
                );
            },

            // 重置表单
            resetForm() {
                this.formData = {};
                this.tableData = [];
            },
            //新增子菜单
            handleAddSon() {
                axios.post("/menu/addSon?roleIds=" + this.roleIds, this.formData).then(res => {
                    //弹出消息
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    //关闭对话框
                    this.dialogFormVisible4Add = false;
                    this.findPage();
                });
            },
            //新增子菜单弹窗
            handleSonCreate(row) {
                this.resetForm();
                this.dialogFormVisible4Add = true;
                this.formData.parentMenuId = row.id;
                //发送请求，获取所有角色信息
                axios.get("/menu/findAllRole").then(resp => {
                    if (resp.data.flag) {
                        //查到了结果
                        //展示表格数据
                        this.tableData = resp.data.data;
                    } else {
                        this.$message.error(resp.data.message);
                    }
                })
            },

            //编辑菜单弹窗
            handleUpdate(row) {
                this.resetForm();
                this.activeName = "first";

                this.dialogFormVisible4Edit = true;
                axios.get("/menu/findMenuById/" + row.id).then(res => {
                    if (!res.data.flag) { //没有查到
                        this.$message.error(resp.data.message);
                        return;
                    }
                    //展示表单数据
                    this.formData = res.data.data;
                    if (this.formData.parentMenuId != null) {
                        this.status = "0";
                        this.status4table = true;
                    }
                    //发送请求查询所有角色
                    axios.get("/menu/findAllRole").then(res => {
                        if (res.data.flag) {
                            //查到了结果
                            //展示表格数据
                            this.tableData = res.data.data;
                        } else {
                            this.$message.error(res.data.message);
                        }
                    });
                    //发送请求获取对应的角色信息
                    axios.get("/menu/findRole/" + row.id).then(resp => {
                        if (!resp.data.flag) {
                            this.$message.error(resp.data.message);
                            return;
                        }
                        this.roleIds = resp.data.data;
                    });
                });
            },

            handleEdit() {
                axios.put("/menu/edit?roleIds=" + this.roleIds, this.formData).then(resp => {
                    //弹出消息
                    this.$message({
                        message: resp.data.message,
                        type: resp.data.flag ? "success" : "error"
                    });
                    //关闭对话框
                    this.dialogFormVisible4Edit = false;
                }).finally(() => {
                    this.findPage();
                })
            },

            //新增菜单弹窗
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
            },
            // 删除
            handleDelete(row) {
                this.$confirm("您确定要删除此记录吗?", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送异步请求
                    axios.delete("/menu/" + row.id).then(resp => {
                        //判断是成功还是失败
                        if (resp.data.flag) {
                            this.$message.success(resp.data.message);
                        } else {
                            this.$message.error(resp.data.message);
                        }
                    }).finally(() => {
                        this.findPage();
                    });
                })
            }
        }
    })
</script>
</html>